package com.alibaba.citrus.cr.platform.enhance.repository.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import com.alibaba.citrus.cr.platform.enhance.repository.ScItemGroupRepository;
import com.alibaba.citrus.cr.platform.enhance.repository.utils.ResultUtils;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.lattice2.epoch.sdk.model.Result;

import com.epoch.app.crplatformenhance.domain.scitemgroup.dto.*;
import com.epoch.app.crplatformenhance.domain.scitemgroup.model.ScItemGroup;
import com.epoch.app.crplatformenhance.domain.scitemgroup.service.ScItemGroupService;
import com.epoch.app.crplatformenhance.dto.request.QueryScItemGroupRequest;
import com.epoch.app.crplatformenhance.dto.response.ScItemGroupResponse;
import com.epoch.app.crplatformenhance.enums.CommStatusEnum;
import com.epoch.app.crplatformenhance.sdo.ScItemGroupSDO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

/**
 * @author zls
 * @description: TODO
 * @date 2021/9/7 18:08
 */
@Component
public class ScItemGroupRepositoryImpl implements ScItemGroupRepository {
    @Resource
    ScItemGroupService scItemGroupService;

    @Override
    public String createScItemGroup(ScItemGroupSDO scItemGroupSDO) {
        //注意，必输字段是统一规范在这检查还是放在纪元方法配置检查?   待确定
//        checkCreateParams(request);
        //数据模型转换  规范待确定
        CreateRequest createRequest = createRequestConverter(scItemGroupSDO);
        createRequest.setStatus(CommStatusEnum.ENABLE.getCode());
        String insertId = ResultUtils.getLastInsertId((Map) scItemGroupService.create(createRequest));
        return insertId;
    }

    private CreateRequest createRequestConverter(ScItemGroupSDO scItemGroupSDO){
        CreateRequest createRequest = new CreateRequest();
        createRequest.setName(scItemGroupSDO.getName());
        createRequest.setCode(scItemGroupSDO.getCode());
        createRequest.setDesc(scItemGroupSDO.getDesc());
        createRequest.setOutCode(scItemGroupSDO.getCode());
        createRequest.setFeatures(scItemGroupSDO.getFeatures());
        return createRequest;
    }

    @Override
    public Boolean editScItemGroup(ScItemGroupSDO scItemGroupSDO) {
        //checkUpdateParams(request);
        UpdateRequest updateRequest = updateRequestConverter(scItemGroupSDO);
        //注意，纪元生成的默认update方法,update条件加上version,,,update数据version+1
        //没有修改会报错...已反馈纪元优化
        Integer count = ResultUtils.getUpdateCount((Map) scItemGroupService.update(updateRequest));
        if(count == 0){
            throw new RuntimeException("数据版本号错误");
        }
        return count > 0;
    }

    private UpdateRequest updateRequestConverter(ScItemGroupSDO scItemGroupSDO){
        UpdateRequest updateRequest = new UpdateRequest();
        updateRequest.setName(scItemGroupSDO.getName());
        updateRequest.setCode(scItemGroupSDO.getCode());
        updateRequest.setDesc(scItemGroupSDO.getDesc());
        updateRequest.setFeatures(scItemGroupSDO.getFeatures());
        updateRequest.setId(scItemGroupSDO.getId());
        return updateRequest;
    }

    @Override
    public ScItemGroupResponse getScItemGroupById(String itemGroupId) {
        LoadRequest loadRequest = new LoadRequest();
        loadRequest.setId(itemGroupId);
        LoadResponse loadResponse = scItemGroupService.load(loadRequest);
        ScItemGroupResponse response = (ScItemGroupResponse) loadResponse.getResult();
        return response;
    }

    @Override
    public ScItemGroupResponse getScItemGroup(QueryScItemGroupRequest request) {
        if (request == null) {
            request = new QueryScItemGroupRequest();
        }
        LoadScItemGroupListRequest loadScItemGroupListRequest = loadListRequestConverter(request);
        Result<List<ScItemGroup>> scItemGroupList = scItemGroupService.loadScItemGroupList(loadScItemGroupListRequest);
        ScItemGroupResponse scItemGroupResponse = new ScItemGroupResponse();
        if (scItemGroupList.getResult() != null && CollectionUtils.isNotEmpty(scItemGroupList.getResult())){
            ScItemGroup scItemGroup = scItemGroupList.getResult().get(0);
            BeanUtils.copyProperties(scItemGroup, scItemGroupResponse);
        }
        return scItemGroupResponse;
    }

    @Override
    public PageResult<List<ScItemGroupSDO>> pageQueryScItemGroupList(QueryScItemGroupRequest request) {
        if (request == null) {
            request = new QueryScItemGroupRequest();
        }
        LoadScItemGroupListRequest loadScItemGroupListRequest = loadListRequestConverter(request);
        Result<List<ScItemGroup>> scItemGroupList = scItemGroupService.loadScItemGroupList(loadScItemGroupListRequest);
        List<ScItemGroupSDO> collect = scItemGroupList.getResult().stream().map(scItemGroup -> {
            ScItemGroupSDO scItemGroupSDO = new ScItemGroupSDO();
            BeanUtils.copyProperties(scItemGroup, scItemGroupSDO);
            scItemGroupSDO.setId(scItemGroup.getId().toString());
            return scItemGroupSDO;
        }).collect(Collectors.toList());
        return PageResult.of(collect, scItemGroupList.getTotal());
    }

    private LoadScItemGroupListRequest loadListRequestConverter(QueryScItemGroupRequest request){
        LoadScItemGroupListRequest loadListRequest = new LoadScItemGroupListRequest();
        BeanUtils.copyProperties(request, loadListRequest);
        loadListRequest.setIdList(request.getIdList());
        if (StringUtils.isNotBlank(request.getId())) {
            loadListRequest.setId(Integer.valueOf(request.getId()));
        }
        return loadListRequest;
    }
}
